package com.heima.user.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.WemediaConstants;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import com.heima.user.service.ApUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    private KafkaTemplate<String,Object> kafkaTemplate;

    @Autowired
    private ApUserService apUserService;

    /**
     * 审核列表
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult getList(AuthDto authDto) {
        if (authDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Page<ApUserRealname> apUserRealnamePage=new Page<>(authDto.getPage(),authDto.getSize());
        LambdaQueryWrapper<ApUserRealname> lqw=new LambdaQueryWrapper<>();
        if (authDto.getStatus()!=null){
            lqw.eq(ApUserRealname::getStatus,authDto.getStatus());
        }
        //排序
        lqw.orderByDesc(ApUserRealname::getCreatedTime);
        page(apUserRealnamePage,lqw);

        PageResponseResult pageResponseResult=new PageResponseResult
                ((int)apUserRealnamePage.getCurrent(),(int)apUserRealnamePage.getSize(),(int)apUserRealnamePage.getTotal());
        pageResponseResult.setData(apUserRealnamePage.getRecords());
        return pageResponseResult;
    }

    /**
     * 审核失败
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult authFail(AuthDto authDto) {
        if (authDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //修改用户状态 和拒绝原因
        update(Wrappers.<ApUserRealname>lambdaUpdate().set(ApUserRealname::getStatus,2)
                .set(ApUserRealname::getReason,authDto.getMsg()).eq(ApUserRealname::getId,authDto.getId()));
        return ResponseResult.okResult("");
    }

    /**
     * 审核成功
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult authPass(AuthDto authDto) {
        if (authDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //修改用户状态
        update(Wrappers.<ApUserRealname>lambdaUpdate().set(ApUserRealname::getStatus,9)
                .eq(ApUserRealname::getId,authDto.getId()));
        // 获取 ApUserRealname
        ApUserRealname apUserRealname = getById(authDto.getId());
        // 获取 ApUser
        ApUser apUser = apUserService.getById(apUserRealname.getUserId());

        //发送消息至自媒体 生成账号
        kafkaTemplate.send(WemediaConstants.WM_USER_CREAT, JSON.toJSONString(apUser));
        return ResponseResult.okResult("");
    }


}
